﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using DTO;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


namespace DAL
{
    public class LOAI_SAN_PHAMDAL : KetNoiDuLieu
    {       

        public void Insert(LOAI_SANPHAM loaisanpham)
        {
            SqlCommand cm = new SqlCommand("sp_loaisp_Insert",LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TEN_LOAI", loaisanpham.Tenloaisp);
            cm.Parameters.AddWithValue("@MA_LOAI_CHA",loaisanpham.Maloaicha);
            cm.Parameters.AddWithValue("@TRANG_THAI", loaisanpham.Trangthai);
            cm.ExecuteNonQuery();
        }

        public void Update(LOAI_SANPHAM loaisanpham)
        {
            SqlCommand cm = new SqlCommand("sp_loaisp_Update", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA_LOAI", loaisanpham.Maloai);
            cm.Parameters.AddWithValue("@TEN_LOAI", loaisanpham.Tenloaisp);
            cm.Parameters.AddWithValue("@MA_LOAI_CHA", loaisanpham.Maloaicha);
            cm.Parameters.AddWithValue("@TRANG_THAI", loaisanpham.Trangthai);
            cm.ExecuteNonQuery();
        }

        public void Update_status(LOAI_SANPHAM loaisanpham)
        {
            SqlCommand cm = new SqlCommand("Update LOAI_SP SET TRANG_THAI = " + loaisanpham.Trangthai.ToString() + " WHERE MA_LOAI = " + loaisanpham.Maloai,LayKetNoi());
            cm.ExecuteNonQuery();
        }

        public void Delete(int maloai)
        {
            SqlCommand cm = new SqlCommand("sp_loaisp_Delete", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA_LOAI", maloai);
            cm.ExecuteNonQuery();
        }

        public Loaisanphamcollection Get(string Where, int PageIndex, int PageSize, string OrderBy,string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_loaisp_GET", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);
            SqlParameter param = new SqlParameter("@TotalRecords", DbType.Int32);
            param.Direction = ParameterDirection.Output;
            cm.Parameters.Add(param);
            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            Loaisanphamcollection loaisanphamcollection = new Loaisanphamcollection();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                LOAI_SANPHAM loaisp = new LOAI_SANPHAM();
                loaisp.Maloai = int.Parse(ds.Tables[0].Rows[i]["MA_LOAI"].ToString());
                loaisp.Tenloaisp = ds.Tables[0].Rows[i]["TEN_LOAI"].ToString();
                loaisp.Maloaicha = int.Parse(ds.Tables[0].Rows[i]["MA_LOAI_CHA"].ToString());
                loaisp.Trangthai = int.Parse(ds.Tables[0].Rows[i]["TRANG_THAI"].ToString());
                loaisanphamcollection.Add(loaisp);
            }
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
           return loaisanphamcollection;
        }
    }
}
